#include "stdafx.h"
#include "CombatLua.h"
#include <iostream>


namespace CombatLua
{

bool push(lua_State* L, ICombatSys* pCombat)
{
	ICombatSys** pp = (ICombatSys**)lua_newuserdata(L, sizeof(ICombatSys*));
	*pp = pCombat;
	luaL_getmetatable(L, "woe.combat");
	if (lua_istable(L, -1))
	{
		lua_setmetatable(L, -2);
		return true;
	}
	else
	{
		LOG_ERROR("Failed to push ICombatSys to lua, metatable not found!");
		return false;
	}
}

ICombatSys* getUserData(lua_State* L)
{
	ICombatSys**pp = (ICombatSys**)luaL_checkudata(L, 1, "woe.combat");
	if (pp==NULL)
	{
		return NULL;
	}
	else
	{
		return *pp;
	}
}

int setFaction(lua_State* L)
{
	ICombatSys* pCombat = getUserData(L);
	if (pCombat)
	{
		pCombat->setFaction(lua_tointeger(L, 2));
	}
	return 0;
}

int getFaction(lua_State* L)
{
	ICombatSys* pCombat = getUserData(L);
	if (pCombat)
	{
		lua_pushinteger(L, pCombat->getFaction());
	}
	return 0;
}

int setHP(lua_State* L)
{
	ICombatSys* pCombat = getUserData(L);
	if (pCombat)
	{
		pCombat->setHP(lua_tointeger(L, 2));
	}
	return 0;
}

int getHP(lua_State* L)
{
	ICombatSys* pCombat = getUserData(L);
	if (pCombat)
	{
		lua_pushinteger(L, pCombat->getHP());
	}
	return 1;
}

int setMaxHP(lua_State* L)
{
	ICombatSys* pCombat = getUserData(L);

	if (pCombat)
	{
		pCombat->setMaxHP(lua_tointeger(L, 2));
	}
	return 0;
}

int getMaxHP(lua_State* L)
{
	ICombatSys* pCombat = getUserData(L);
	if (pCombat)
	{
		lua_pushinteger(L, pCombat->getMaxHP());
	}
	return 1;
}

int setMP(lua_State* L)
{
	ICombatSys* pCombat = getUserData(L);
	if (pCombat)
	{
		pCombat->setMP(lua_tointeger(L, 2));
	}
	return 0;
}

int getMP(lua_State* L)
{
	ICombatSys* pCombat = getUserData(L);
	if (pCombat)
	{
		lua_pushinteger(L, pCombat->getMP());
	}
	return 1;
}

int setMaxMP(lua_State* L)
{
	ICombatSys* pCombat = getUserData(L);
	if (pCombat)
	{
		pCombat->setMaxMP(lua_tointeger(L, 2));
	}
	return 0;
}

int getMaxMP(lua_State* L)
{
	ICombatSys* pCombat = getUserData(L);
	if (pCombat)
	{
		lua_pushinteger(L, pCombat->getMaxMP());
	}
	return 1;
}

int setProps(lua_State* L)
{
	ICombatSys* pCombat = getUserData(L);
	if (pCombat)
	{
		short idx = luaL_checkint(L, 2);
		int val = luaL_checkint(L, 3);
		pCombat->setProps(idx, val);
	}
	return 0;
}

int getProps(lua_State* L)
{
	ICombatSys* pCombat = getUserData(L);
	if (pCombat)
	{
		short idx = luaL_checkint(L, 2);
		lua_pushinteger(L, pCombat->getProps(idx));
	}
	return 1;
}


int setPropVit(lua_State* L)
{
	ICombatSys* pCombat = getUserData(L);
	if (pCombat)
	{
		int val = luaL_checkint(L, 2);
		pCombat->setProps(prop_vit, val);
	}
	return 0;
}

int getPropVit(lua_State* L)
{
	ICombatSys* pCombat = getUserData(L);
	if (pCombat)
	{
		lua_pushinteger(L, pCombat->getProps(prop_vit));
	}
	return 1;
}

int setPropStr(lua_State* L)
{
	ICombatSys* pCombat = getUserData(L);
	if (pCombat)
	{
		int val = luaL_checkint(L, 2);
		pCombat->setProps(prop_str, val);
	}
	return 0;
}

int getPropStr(lua_State* L)
{
	ICombatSys* pCombat = getUserData(L);
	if (pCombat)
	{
		lua_pushinteger(L, pCombat->getProps(prop_str));
	}
	return 1;
}


int setPropAgi(lua_State* L)
{
	ICombatSys* pCombat = getUserData(L);
	if (pCombat)
	{
		int val = luaL_checkint(L, 2);
		pCombat->setProps(prop_agi, val);
	}
	return 0;
}

int getPropAgi(lua_State* L)
{
	ICombatSys* pCombat = getUserData(L);
	if (pCombat)
	{
		lua_pushinteger(L, pCombat->getProps(prop_agi));
	}
	return 1;
}


int setPropInt(lua_State* L)
{
	ICombatSys* pCombat = getUserData(L);
	if (pCombat)
	{
		int val = luaL_checkint(L, 2);
		pCombat->setProps(prop_int, val);
	}
	return 0;
}

int getPropInt(lua_State* L)
{
	ICombatSys* pCombat = getUserData(L);
	if (pCombat)
	{
		lua_pushinteger(L, pCombat->getProps(prop_int));
	}
	return 1;
}



static const luaL_Reg funcs[] = {
	{"setHP", setHP},
	{"getHP", setHP},
	{"setMaxHP", setMaxHP},
	{"getMaxHP", setMaxHP},
	{"setMP", setMP},
	{"getMP", getMP},
	{"setMaxMP", setMaxMP},
	{"getMaxMP", getMaxMP},
	{"setProps", setProps},
	{"getProps", getProps},
	{"setPropVit", setPropVit},
	{"getPropVit", getPropVit},
	{"setPropStr", setPropStr},
	{"getPropStr", getPropStr},
	{"setPropAgi", setPropAgi},
	{"getPropAgi", getPropAgi},
	{"setPropInt", setPropInt},
	{"getPropInt", getPropInt},
	{"setFaction", setFaction},
	{"getFaction", getFaction},
	{NULL, NULL}
};


int libaray(lua_State* L)
{
	luaL_newmetatable(L, "woe.combat");
	// fill member list into metatable
	luaL_register(L, NULL, funcs);

	// metatable.__index = metatable
	lua_pushstring(L, "__index");
	lua_pushvalue(L, -2);
	lua_settable(L, -3);

	return 0;
}


}	//end combat lua